const express = require('express')
const app = express()
const port = 3021

//引入路由文件
const router = require('./router.js')

const bodyParser = require('body-parser')

//开启静态文件
app.use('/www',express.static('public'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));

//使用http-proxy-middleware
// var proxy = require('http-proxy-middleware');

// app.use(
//   '/api',
//   proxy({ target: 'http://www.example.org', changeOrigin: true })
// );
// var apiProxy = proxy('http://www.example.org/api');

// var options = {
//   target: 'http://www.example.org', // 目标
//   changeOrigin: true, // 虚拟站点必须
//   ws: true, // 代理websocket
//   pathRewrite: {
//     '^/api/old-path': '/api/new-path', // 重写路径
//   },
//   router: {
//     // when request.headers.host == 'dev.localhost:3000',
//     // override target 'http://www.example.org' to 'http://localhost:8000'
//     'dev.localhost:3000': 'http://localhost:8000'
//   }
// };









app.get('/', (req, res) => res.send('Hello World!'))

app.all('*', function(req, res, next) {
            res.header("Access-Control-Allow-Origin", "*");
            res.header("Access-Control-Allow-Headers","*");
            res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
			 res.header("Access-Control-Allow-Credentials",'true')
            res.header("X-Powered-By",' 3.2.1')
            res.header("Content-Type", "application/json;charset=utf-8");
           
            next();
});

app.use(router)

app.listen(port, () => {
	console.log(`running.......... on port ${port}!`)
})